﻿using Quartz;
using System;
using System.Threading.Tasks;
using Zhao.IServer.Events;
using Zhao.IServer.Log;

namespace Zhao.Arena.Utility.QuartZJob
{
    /// <summary>
    /// 活动状态修改定时任务类
    /// </summary>
    public class ActivitiesStatusJbo : IJob
    {
        private readonly IActivitiesServer _activityService;
        private readonly ILogsServer _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志服务</param>
        public ActivitiesStatusJbo(IActivitiesServer activityService, ILogsServer logger)
        {
            _activityService = activityService;
            _logger = logger;
        }

        /// <summary>
        /// 执行定时任务，更新活动状态
        /// </summary>
        /// <param name="context">任务上下文</param>
        /// <returns></returns>
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                // 调用服务层方法更新活动状态
                int updateCount = await _activityService.UpdateActivitiesStatusAsync();
                await _logger.Info($"活动状态定时任务已执行：更新了{updateCount}条记录");
            }
            catch (Exception ex)
            {
                await _logger.Error($"活动状态定时任务执行失败：{ex.Message}");
                throw; // 重新抛出异常以便Quartz可以处理作业失败
            }
        }
    }
}
